package com.softer.chengxin.district.dao;


import com.softer.chengxin.district.entity.District;
import com.softer.chengxin.district.entity.DistrictStreet;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zcz
 * @date 2018/12/20
 */
@Mapper
@Repository
public interface DistrictDao {

    /**
     * 查询地区树
     * @param id
     * @return
     */
    @Select("SELECT id,name AS label FROM district_street where parent_id = #{id}")
    List<DistrictStreet> getDistrictStreet(Integer id);
    /**
     * 根据parentId查询地区列表
     * @param parentId
     * @return
     */
    @Select("select id,name,parent_id AS parentId from district_street where parent_id = #{parentId}")
    List<District> getDistrictByParentId(String parentId);

    /**
     * 新增
     * @param district
     * @return
     */
    @Insert("INSERT INTO district_street(gmt_create,gmt_modified,name,parent_id) VALUES(NOW(),NOW(),#{name},#{parentId})")
    int add(District district);

    /**
     * 查询id
     * @return count
     */
    @Select("select id from district_street where name = #{name} and parent_id = #{parentId}")
    int getId(@Param("name") String name,@Param("parentId")String parentId);

    /**
     * 根具id查詢區域信息，用來填充修改框
     * @return count
     */
    @Select("SELECT id,name FROM district_street WHERE id =  #{id}")
    District getDistrictById(String id);
    /**
     * 查询id
     * @return count
     */
    @Select("select count(id) from district_street where name = #{name} and parent_id = #{parentId}")
    int getCountId(@Param("name") String name,@Param("parentId")String parentId);

    /**
     * 修改
     * @param district
     * @return
     */
    @Update("UPDATE district_street SET gmt_modified = NOW(),name = #{name} WHERE id =#{id}")
    int update(District district);

    /**
     * 删除
     * @param id
     * @return
     */
    @Delete("DELETE FROM district_street WHERE id = #{id}")
    int delete(String id);
    /**
     * 根据id查询是否有子区域
     * @return count
     */
    @Select("SELECT count(id) from district_street WHERE parent_id = #{id}")
    int getCountByParentId(String id);


}
